﻿using System;
using TMPro;
using TrueSync;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;

namespace HEFramework
{
    public static class HUDTextSystem
    {
        [EntitySystem]
        public class HUDTextAwakeSystem : AwakeSystem<HUDText>
        {
            protected override void Awake(HUDText _self)
            {
                _self.Index = 0;
            }
        }

        [EntitySystem]
        public class HUDTextUpdateSystem : UpdateSystem<HUDText>
        {
            protected override void Update(HUDText _self)
            {
                float deltaTime = Time.deltaTime;

                for (int i = 0; i < _self.Elements.Count; i++)
                {
                    var element = _self.Elements[i];

                    if (element.IsFree || !element.IsActive)
                    {
                        continue;
                    }

                    if (element.Text == null)
                    {
                        continue;
                    }

                    element.Object.transform.Translate(0, element.Speed * deltaTime, 0); //使字体向上移动

                    if (element.Speed > 0)
                    {
                        element.Speed -= element.Speed_Weak; //使得向上移动的速度逐渐减少
                    }
                    else if (element.Speed <= 0 && element.Weak_Time > 0)
                    {
                        element.Weak_Time -= deltaTime; //当速度为0后，字体颜色开始透明的时间
                    }
                    else if (element.Alpha > 0 && element.Weak_Time <= 0)
                    {
                        element.Alpha -= 0.01f * element.Alpha_Weak; //控制字体逐渐透明
                        if (element.Alpha <= 0)
                        {
                            element.Alpha = 0;
                            element.IsActive = false;
                            element.IsFree = true;
                        }
                    }

                    //修改颜色，透明度等
                    var color = new Color(element.Text.color.r, element.Text.color.g, element.Text.color.b, element.Alpha);
                    element.Text.color = color;
                    element.Icon.color = color;
                }

                for (int i = 0; i < 10; i++)
                {
                    _self.Check();
                }
            }
        }

        [EntitySystem]
        public class HUDTextDestroySystem : DestroySystem<HUDText>
        {
            protected override void Destroy(HUDText _self)
            {
                var pc = _self.GetSceneChild<PrefabComponent>();
                if (pc == null)
                {
                    return;
                }

                for (int i = 0; i < _self.Elements.Count; i++)
                {
                    pc.Recycle("2d/prefabs/uiitem", "HUDTextItem", _self.Elements[i].Text.gameObject);
                }

                _self.Elements.Clear();
            }
        }


        public static void Check(this HUDText _self)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            var sc = _self.GetSceneChild<Statistic>();
            if (dc == null || sc == null)
            {
                return;
            }

            float fs_0 = 2f;
            float fs_1 = 3f;

            if (_self.Index < sc.StatisticDatas.Count)
            {
                var data = sc.StatisticDatas[_self.Index];
                _self.Index++;

                if (data.Type != Statistic.eType.Battle)
                {
                    return;
                }

                if (!data.IsShow)
                {
                    return;
                }


                var h = dc.LoadDataTable<DRAttribute>().GetData(data.B_ID).B_H;
                var y = Formula.LG(h / 2) / FP.Sqrt(2);
                Vector3 pos = data.Pos.ToVector() + new Vector3(0, (float)y, (float)-y);
                float rX = Random.Range(-0.2f, 0.2f);
                float rY = Random.Range(-0.1f, 0.1f);
                Vector3 of = new(-0.1f + rX, 0.5f + rY);
                pos += of;
                HUDText.HUDTextElement element;
                if (data.HP != 0)
                {
                    switch (data.PowerType)
                    {
                        case ePowerType.Physics:
                            if (data.IsCritical)
                            {
                                Color color = data.HP < 0 ? Graphics.COLOR_MORE_RED : Graphics.COLOR_MORE_GREEN;
                                element = _self.Pop(Math.Abs((int)data.HP).ToString(), fs_1, color, pos);
                                element.Icon.gameObject.SetActive(true);
                            }
                            else
                            {
                                Color color = data.HP < 0 ? Graphics.COLOR_RED : Graphics.COLOR_GREEN;
                                element = _self.Pop(Math.Abs((int)data.HP).ToString(), fs_0, color, pos);
                                element.Icon.gameObject.SetActive(false);
                            }

                            break;

                        case ePowerType.Magic:
                            if (data.IsCritical)
                            {
                                Color color = data.HP < 0 ? Graphics.COLOR_MORE_BLUE : Graphics.COLOR_MORE_GREEN;
                                element = _self.Pop(Math.Abs((int)data.HP).ToString(), fs_1, color, pos);
                                element.Icon.gameObject.SetActive(true);
                            }
                            else
                            {
                                Color color = data.HP < 0 ? Graphics.COLOR_BLUE : Graphics.COLOR_GREEN;
                                element = _self.Pop(Math.Abs((int)data.HP).ToString(), fs_0, color, pos);
                                element.Icon.gameObject.SetActive(false);
                            }

                            break;
                    }
                }

                if (data.IsMiss)
                {
                    element = _self.Pop("Miss", fs_0, Graphics.COLOR_WHITE, pos);
                    element.Icon.gameObject.SetActive(false);
                }
            }
        }


        public static HUDText.HUDTextElement Pop(this HUDText _self, string _v, float _si, Color _c, Vector3 _p, float _s = 5f, float _sw = 0.4f, float _wt = 0.2f, float _aw = 2f, bool _isActive = true)
        {
            return _self.GetText(_v, _si, _c, _p, _s, _sw, _wt, _aw, _isActive);
        }

        private static HUDText.HUDTextElement GetText(this HUDText _self, string _v, float _si, Color _c, Vector3 _p, float _s = 5f, float _sw = 0.4f, float _wt = 0.2f, float _aw = 2f, bool _isActive = true)
        {
            var pc = _self.GetSceneChild<PrefabComponent>();
            if (pc == null)
            {
                return null;
            }

            var hud = (_self.Parent as HUD);
            Transform parent = hud.UIWindowBehaviour.GetTransform();
            HUDText.HUDTextElement element = null;
            var elements = _self.Elements;
            for (int i = 0; i < elements.Count; i++)
            {
                if (elements[i].IsFree)
                {
                    element = elements[i];
                    break;
                }
            }

            if (element == null)
            {
                element = new HUDText.HUDTextElement();
                var obj = pc.GetPrefabSyn("2d/prefabs/uiitem", "HUDTextItem");
                obj.transform.SetParent(parent.transform);
                obj.transform.localPosition = Vector2.zero;
                element.Object = obj;
                element.Text = obj.GetComponent<TransformBehaviour>().GetTransform("Transform_Text").GetComponent<TMP_Text>();
                element.Icon = obj.GetComponent<TransformBehaviour>().GetTransform("Transform_Icon").GetComponent<Image>();
                elements.Add(element);
            }

            element.Text.text = _v;
            element.Text.fontSize = _si;
            element.Text.color = _c;
            element.Icon.color = _c;

            element.Speed = _s;
            element.Speed_Weak = _sw;
            element.Weak_Time = _wt;
            element.Alpha = 1;
            element.Alpha_Weak = _aw;


            element.Object.transform.localPosition = hud.SyncItemUIPos(_p);


            element.IsFree = false;
            element.IsActive = _isActive;
            return element;
        }
    }
}